﻿--个人维护的百度客户
CREATE  PROCEDURE [dbo].[proc_BDCus_Getlist_Track]
	(
		@CusName nvarchar(300),
		@Companyid int,
		@UserName nvarchar(50),		
		@StartIndex int,
		@EndIndex int
	)
AS
begin
	
	declare @sql varchar(max),@swhere nvarchar(500)
	
	if @CusName<>''
		set @swhere=' and  CustomerName like ''%'+@CusName+'%'' '
	else
		set @swhere=''
	
	set @sql='
	;WITH list As (Select ROW_NUMBER() OVER (ORDER BY TrackDate DESC) AS Row,
		Id,
		delFlag,
		CustomerId,
		isnull(CustomerName,''...'') as CustomerName,
		Project,
		ProtectId,
		SourceId,
		IndustryOneId,
		TypeId,
		CreateDate,
		
		IsNull(ProtectDate,''1900-01-01'') As ProtectDate		
		,ProtectName
	From Customer Where companyid='+Convert(varchar,@Companyid)+' and delflag=0 and  BDServicePerson = '''+@UserName+''' and  Audit=1 '+@swhere+' )
	
	Select *,(Select Count(0) From list) As RecordCount,
	
		isnull((Select Title From Customer_Source Where Id=list.SourceId),''...'') As Source,
		isnull((Select Title From Customer_Industry Where Id=list.IndustryOneId),''...'') As IndustryOne,
		isnull((Select Title From Customer_Type Where Id=list.TypeId),''...'') As Type,
		ISNULL((Select Title From Customer_Protect Where Id=list.ProtectId),''...'') As Protect,
		isnull((select Ename from employee where employee.username=ProtectName),'''') ProtectNameEName
	
	 From list 
		Where Row Between '+Convert(varchar,@StartIndex)+' and '+Convert(varchar,@EndIndex)+'  Order By Row
		'
exec(@sql)	
--print (@sql)	
end
